package com.kingtone.rongji.fragment;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ExpandableListView;

import com.kingtone.rongji.R;
import com.kingtone.rongji.adapter.LeftCategoryAdapter;
import com.kingtone.rongji.biz.LeftCategory;
import com.kingtone.rongji.biz.ProductList;
import com.kingtone.rongji.network.LeftCategoryRequest;

public class LeftCategoryFragment extends BaseFragment implements OnClickListener {

	private static final String TAG = "LeftMenuFragment";

	private Activity mContext;

	private List<String> groupArray  = new ArrayList<String>();

	private List<List<String>> childArray = new ArrayList<List<String>>();
	
	private List<LeftCategory> categories;
	
	private LeftCategoryAdapter adapter;

	@Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		Log.i(TAG, "onAttach");
		super.onAttach(activity);
		this.mContext = activity;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		Log.i(TAG, "onCreateView");
		initData();

		View view = inflater.inflate(R.layout.fragment_left_category, null);

		ExpandableListView expandableListView = (ExpandableListView) view
				.findViewById(R.id.expandableListView);
		adapter = new LeftCategoryAdapter(mActivity, groupArray, childArray);
		expandableListView.setAdapter(adapter);

		return view;
	}

	private void initData() {
		LeftCategoryRequest categoryRequest = new LeftCategoryRequest();
		categoryRequest.getAllCategory(3, this);
	}

	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onBussinessSuccess(Object obj) {
		// TODO Auto-generated method stub
		super.onBussinessSuccess(obj);
		if(obj != null)
		{
			categories = (List<LeftCategory>) obj;
			if(categories != null)
			{
				Iterator<LeftCategory> it = categories.iterator();
				while (it.hasNext()) {
					LeftCategory category = it.next();
					String name = category.getParentCategoryName();
					if(!groupArray.contains(name))
					{
						groupArray.add(name);
						//it.remove();
					}
				}
			}

			for (int index = 0; index < groupArray.size(); ++index) {
				for (int i = 0; i < categories.size(); i++)
				{
					LeftCategory category = categories.get(i);
					String name = category.getParentCategoryName();
					if(groupArray.get(index).equals(name))
					{
						if(childArray.size() < index + 1)
						{
							childArray.add(index, new ArrayList<String>());
						}
						
						childArray.get(index).add(category.getCategoryId() + "_#_" + category.getCategoryName());
					}
				}
			}

			adapter.notifyDataSetChanged();
		}
	}
}
